package com.passionfruit.module.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.passionfruit.module.entity.SysRoleMenu;
import com.passionfruit.module.entity.SysUserRole;
import com.passionfruit.module.mapper.SysRoleMenuMapper;
import com.passionfruit.module.service.ISysRoleMenuService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色与菜单对应关系 服务实现类
 * </p>
 *
 * @author weirdor
 * @since 2017-10-31
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {

    @Override
    public void saveOrUpdate(Long roleId, List<Long> menuIdList) {
        //先删除角色与菜单关系
        EntityWrapper<SysRoleMenu> ew=new EntityWrapper<>();
        ew.eq("role_id",roleId);
        super.delete(ew);

        if(menuIdList.size() == 0){
            return ;
        }
        //保存角色与菜单关系
        menuIdList.forEach(k->{
            SysRoleMenu sysRoleMenu=new SysRoleMenu();
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setMenuId(k.longValue());
            super.insertOrUpdate(sysRoleMenu);
        });
    }

    @Override
    public List<Long> queryMenuIdList(Long roleId) {
        List<Long> longList=new ArrayList<>();
        EntityWrapper<SysRoleMenu> ew=new EntityWrapper<>();
        ew.eq("role_id",roleId);
        List<SysRoleMenu> sysUserRoles= super.selectList(ew);
        sysUserRoles.forEach(k ->{
            longList.add(k.getMenuId());
        });
        return longList;
    }

}
